$(function(){
	$("#note").ajaxSend(function(evt, request, settings){		
    	$(this).prepend("<i>正在请求: " + settings.url + "&" + settings.data + "</i><hr/>");
 	});
	$("#note").ajaxSuccess(function(evt, request, settings){
		var json = $.parseJSON(request.responseText);
		if(json.state == 0){
			$(this).prepend("<i>"+json.msg+"</i>");	
		}
		if($(this).text().length > 50000) $(this).text('');
	});
	$("#note").ajaxError(function(event,request, settings){
    	$(this).prepend("<li>出错页面:" + settings.url + "</li>");
		$(this).prepend(request.responseText);
	});
		   
	$("#j_list").bind("click", function(){ pick.getInstance().init(); pick.getInstance().pickList(); } );
	$("#j_info").bind("click", function(){ pick.getInstance().init(); pick.getInstance().pickInfo(); } );	
	$("#j_item").bind("click", function(){ pick.getInstance().init(); pick.getInstance().pickItemInfo(); } );	
	$("#j_stop").bind("click", function(){ pick.getInstance().stop = true; } );	
});

function pick()
{	
	this.stop = false;
	this.intTry  = 0;
	this.classArray  = new Array('class1','class2','class3','class4','class5','class6','class7','class8','class9','class10','class11');
	this.currClassID = null;
}

pick.instance=null;
pick.getInstance = function(){
	if(pick.instance==null){
		pick.instance = new pick();
	}
	return pick.instance;
}

pick.prototype.init = function(){	
	this.url = "/?c=admin&a=picking";
	this.stop = false;
	this.page = 1;
	this.intTry = 0; //尝试次数-
}

//采集列表
pick.prototype.pickList = function(){
	if(this.stop){ $("#note").prepend("<b>用户中断操作.</b><br><br>"); return;}
	if( this.currClassID == '' || this.currClassID == null || this.currClassID == undefined ){
		this.currClassID = this.classArray.pop();
	}
	if( this.currClassID == '' || this.currClassID == null || this.currClassID == undefined ){
		alert( this.currClassID );
		$("#note").prepend("<b>采集列表结束.</b><br><br>"); 
		return;
	}
	$.ajax({
		type: "POST",
		dataType: "json",
		url: this.url,
		data: {do:'picklist', classid:this.currClassID, page:this.page},
		success: function(json){
			$("#note").prepend("<hr>");
			if(json.state == 1 || json.state == 2 || json.state == 3){
				$.each(json.data, function(i, n){
					$("#note").prepend("Item #" + i + ": " + n + "<br>");
				});
				if(json.state == 1){
					pick.getInstance().page++;
					pick.getInstance().pickList();
				}
				else if(json.state == 2){
					if( pick.getInstance().intTry < 1000 ){
						$("#note").prepend("<i>2.未发现新记录，尝试继续请求下一页！</i><hr/>");	
						pick.getInstance().intTry++;
						pick.getInstance().page++;
						pick.getInstance().pickList();
					}
					else{
						$("#note").prepend("<i>2.未发现新记录，继续下个栏目！</i><hr/>");	
						pick.getInstance().intTry = 0;
						pick.getInstance().page = 1;
						pick.getInstance().currClassID =  pick.getInstance().classArray.pop();
						pick.getInstance().pickList();
					}
				}
				else{
					$("#note").prepend("<i>3.未发现新记录，继续下个栏目！</i><hr/>");	
					pick.getInstance().intTry = 0;
					pick.getInstance().page = 1;
					pick.getInstance().currClassID =  pick.getInstance().classArray.pop();
					pick.getInstance().pickList();	
				}
			}					
		}
	});
}

//采集内容
pick.prototype.pickInfoDelay = function()
{
	var delay = Math.ceil(3*Math.random());
	$("#note").prepend("<i>延迟"+delay+" s...</i><br/>");		
	setTimeout(function(){ pick.getInstance().pickInfo(); }, delay*1000);
}
pick.prototype.pickInfo = function(){
	if(this.stop){ $("#note").prepend("<b>用户中断操作.</b><br><br>"); return;}
	$.ajax({
		type: "POST",
		dataType: "json",
		url: this.url,
		data: {do:'pickinfo'},
		success: function(json){
			$("#note").prepend("<hr>");
			if(json.state == 3){
				$("#note").prepend("<i>采集结束！</i><hr/>");		
			}
			if(json.state == 1 || json.state == 2){
				$.each(json.data, function(i, n){
					$("#note").prepend("Item #" + i + ": " + n + "<br>");
				});
				if(json.state == 1){					
					pick.getInstance().pickInfoDelay();
				}
				else{
					if( pick.getInstance().intTry < 50 ){
						$("#note").prepend("<i>未发现新记录，尝试继续请求下条记录！</i><hr/>");	
						pick.getInstance().intTry++;						
						pick.getInstance().pickInfoDelay();
					}
					else{
						$("#note").prepend("<i>未发现新记录，请求结束！</i><hr/>");	
					}
				}
			}					
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			if( pick.getInstance().intTry < 100 ){
				$("#note").prepend("<i>请求失败，尝试继续请求下条记录！</i><hr/>");	
				pick.getInstance().intTry++;						
				pick.getInstance().pickInfoDelay();
			}
			else{
				$("#note").prepend("<i>请求失败次数>50，请求结束！</i><hr/>");	
			}
		}
	});
}

//采集章节内容
pick.prototype.pickItemInfo = function(){
	if(this.stop){ $("#note").prepend("<b>用户中断操作.</b><br><br>"); return;}
	$.ajax({
		type: "POST",
		dataType: "json",
		url: this.url,
		data: {do:'pickiteminfo'},
		success: function(json){
			$("#note").prepend("<hr>");
			if(json.state == 3){
				$("#note").prepend("<i>采集结束！</i><hr/>");		
			}
			if(json.state == 1 || json.state == 2){
				$.each(json.data, function(i, n){
					$("#note").prepend("Item #" + i + ": " + n + "<br>");
				});
				pick.getInstance().pickItemInfo();
				/*var delay = Math.ceil(2*Math.random());
				$("#note").prepend("<i>延迟"+delay+" s...</i><br/>");			
				setTimeout(function(){ pick.getInstance().pickItemInfo(); }, delay*1000);*/
			}					
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			$("#note").prepend(textStatus);	
			pick.getInstance().pickItemInfo();
		}
	});
}
